"""
CARLA Dynamic Weather:

Connect to a CARLA Simulator instance and control the weather. Change Sun
position smoothly with time and generate storms occasionally.
"""

import glob
import os
import sys

import carla

import argparse
import math


def main():
    argparser = argparse.ArgumentParser(description=__doc__)
    argparser.add_argument(
        '--host',
        metavar='H',
        default='127.0.0.1',
        help='IP of the host server (default: 127.0.0.1)',
    )
    argparser.add_argument(
        '-p',
        '--port',
        metavar='P',
        default=2000,
        type=int,
        help='TCP port to listen to (default: 2000)',
    )
    argparser.add_argument(
        '-s',
        '--speed',
        metavar='FACTOR',
        default=1.0,
        type=float,
        help='rate at which the weather changes (default: 1.0)',
    )
    args = argparser.parse_args()
    while True:
        client = carla.Client(args.host, args.port)
        client.set_timeout(5.0)
        world = client.get_world()
        actors = world.get_actors()
        print(actors)
        vehicle_actor_list = actors.filter('*')
        print(len(vehicle_actor_list))
        vehicle_actor = vehicle_actor_list[0]
        transform = vehicle_actor.get_transform()
        spectator = world.get_spectator()
        spectator.set_transform(
            carla.Transform(
                transform.location + carla.Location(z=300), carla.Rotation(pitch=-90)
            )
        )


if __name__ == '__main__':
    main()
